/******************************************************************************
******************************************************************************
******************************************************************************

This file reproduces the Tables and Figures for:
"Rejecting Ethnic Pandering in Urban Africa: A Survey Experiment on Voter Preferences in Nairobi, Kenya"

Created: July 29, 2021

To execute this file, create a folder named "Data" in the working directory; place the four datasets listed below in 
the Data folder; and update the file path to the working directory in line 39

This code uses several datasets:
 - 2015 survey data.dta : the main analysis data 
 - 2016 survey data.dta : data from a replication study
 - 2012 survey data.dta : data used to examine social networks in Nairobi
 - urb_rur_data.data : data from multiple surveys used to estimate group bloc voting rates in Nairobi vs. rest of country
 	

******************************************************************************
******************************************************************************
******************************************************************************/

** Code requires dependencies

* Requires ciplot -- install if needed
ssc install ciplot 

** Requires outreg2 -- install if needed
ssc install outreg2

** Requires coefplot -- install if needed
ssc install coefplot

******************************************************************************
** Set working directory and open data

** Set working directory to the replication folder on user's computer
cd " "

** Create folder to save output
capture rmdir "paper_output"
mkdir "paper_output"






********************************************************************
**************************** FIGURE 1 ******************************
********************************************************************

use "Data/urb_rur_data.dta", replace	

gen vote100=group_voter*100
gen vote100_nairobi=vote100
replace vote100_nairobi=. if nairobi==0
gen vote100_other=vote100
replace vote100_other=. if nairobi==1

gen eg_cat=.
replace eg_cat=1 if eg=="kikuyu"
replace eg_cat=2 if eg=="luo"
replace eg_cat=3 if eg=="luhya"
replace eg_cat=4 if eg=="kamba"
replace eg_cat=5 if eg=="kisii"
replace eg_cat=6 if eg=="kalenjin"
replace eg_cat=7 if eg=="meru"
replace eg_cat=8 if eg=="somali"

lab define eg_catl 1 "Kikuyu" 2 "Luo" 3 "Luhya" 4 "Kamba" 5 "Kisii" 6 "Kalenjin" 7 "Meru" 8 "Somali"
lab value eg_cat eg_catl

graph bar (mean) vote100_nairobi vote100_other , over(eg_cat)  ///
legend(on pos(6) col(2) symxsize(*.5) label(2 "Rest of Country") label(1 "Nairobi") )  blabel(total, format(%8.0f)) ///
bar(1, fcolor(gs4) color(black)) ///
scheme(s1mono) plotregion(lcolor(white)) ylabel(, angle(0)) yscale(noextend) ylab(0(20)100, nogrid) scale(1.5) xsize(10) aspectratio(.25) 

graph export paper_output/figure1.pdf, as(pdf) replace	




********************************************************************
**************************** FIGURE 2 ******************************
********************************************************************


use "Data/2012 survey data.dta", clear

recode networknco (0=0) (1/4=1), gen(nco_di)  // creates dichotomous variable with value of 1 if R reports 1 or more non-coethnic in social network

gen length_nairobi=q104_r1 if nairobi_r1==1 // length of time in Nairobi
xtile length_5 = length_nairobi, nq(5) // by quintiles

ciplot nco_di, by(length_5) scheme(s1mono) plotregion(lc(white)) ytitle("Share reporting 1 or more non-coethnic in network") ///
xlabel(2 `""q1" "[1 year or less]""' 5 `""q2" "[2-3 years]""'  8 `""q3" "[4-6 years]""'  11 `""q4" "[8-12 years]""'  14 `""q5" "[14-36 years]""') ///
xtitle("Length in Nairobi (quintiles)", margin(t=5)) ylabel(, angle(0))

graph export paper_output/figure2.pdf, as(pdf) replace	



********************************************************************
**************************** FIGURE 3 ******************************
********************************************************************

use "Data/2015 survey data.dta", clear

drop if target_eg==0 // excludes respondents from groups not included in the survey experiment

gen support_mean=.
gen se_support_mean=.

mean support_candidate if treat==0 & target_eg==1 & coeth_cand==0
mat b = r(table)
replace support_mean = b[1,1] if _n==1
replace se_support_mean = b[2,1] if _n==1

mean support_candidate if treat==1 & target_eg==1 & coeth_cand==0
mat b = r(table)
replace support_mean = b[1,1] if _n==2
replace se_support_mean = b[2,1] if _n==2

gen support_low = support_mean - (1.962* se_support_mean)
gen support_hi = support_mean + (1.962* se_support_mean)

gen groupnum=_n in 1/2

twoway (bar support_mean groupnum, fcolor(gs12) lcolor(black) barw(.7)) || (rcap support_low support_hi groupnum, lcolor(gs6)), ///
scheme(s1mono) ylabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5", angle(0)) ///
xlabel(1 `""Inclusive" "appeal""' 2 `""Targeted" "appeal""') ///
xtitle("") legend(off) xsize(2) plotregion(lcolor(white)) title("Non-coethnic" "candidate", margin(b=5)) name(g1, replace) ///
text(4.50 1 "4.07") text(3.62 2 "3.16") text(5 1.5 "|------ -0.91** ------|")

gen support_mean_t=.
gen se_support_mean_t=.

mean support_candidate if treat==0 & target_eg==1 & coeth_cand==1
mat b = r(table)
replace support_mean_t = b[1,1] if _n==1
replace se_support_mean_t = b[2,1] if _n==1

mean support_candidate if treat==1 & target_eg==1 & coeth_cand==1
mat b = r(table)
replace support_mean_t = b[1,1] if _n==2
replace se_support_mean_t = b[2,1] if _n==2

reg support_candidate treat if target_eg==1 & coeth_cand==1
ttest support_candidate if target_eg==1 & coeth_cand==0, by(treat)

gen support_low_t = support_mean_t - (1.962* se_support_mean_t)
gen support_hi_t = support_mean_t + (1.962* se_support_mean_t)

twoway (bar support_mean_t groupnum, fcolor(gs12) lcolor(black) barw(.7)) || (rcap support_low_t support_hi_t groupnum, lcolor(gs6)), ///
scheme(s1mono) ylabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5", angle(0)) ///
xlabel(1 `""Inclusive" "appeal""' 2 `""Targeted" "appeal""') ///
xtitle("") legend(off) xsize(2) plotregion(lcolor(white)) title("Coethnic"  "candidate", margin(b=5)) name(g2, replace) ///
text(4.52 1 "4.08") text(3.8 2 "3.29") text(5 1.5 "|------ -0.79** ------|")

graph combine g2 g1, ycommon scheme(s1mono) scale(1.5) imargin(large)  xsize(7) ysize(4.4)
graph export paper_output/figure3.pdf, as(pdf) replace	




********************************************************************
**************************** FIGURE 4 ******************************
********************************************************************


reg represent_recode i.treat##i.coeth_cand if target_eg==1, robust
margins, dydx(treat) at(coeth_cand = (1 0)) post
eststo m1

reg good_democ_recode i.treat##i.coeth_cand if target_eg==1, robust
margins, dydx(treat) at(coeth_cand = (1 0)) post
eststo m2

reg good_peace_recode i.treat##i.coeth_cand if target_eg==1, robust
margins, dydx(treat) at(coeth_cand = (1 0)) post
eststo m3

reg fulfill_promises_recode i.treat##i.coeth_cand if target_eg==1, robust
margins, dydx(treat) at(coeth_cand = (1 0)) post
eststo m4


coefplot m1 m2 m3 m4, scheme(s1mono) vertical yline(0, lc(red) lp(dash)) plotregion(lc(white)) ///
xlabel(1 `""Coethnic" "candidate""' 2 `""Non-coethnic" "candidate""') ylabel(-1.4(.2).2, angle(0)) ///
ytitle("Marginal effect," "relative to inclusive appeal") ///
legend(row(1) label(2 "Represent") label(4 "Good for" "Democracy") label(6 "Good for" "peace") ///
label(8 "Fulfill" "promises") stack  symplacement(center)  symxsize(*1.5) symysize(*1.5))

graph export paper_output/figure4.pdf, as(pdf) replace	




********************************************************************
**************************** FIGURE 5 ******************************
********************************************************************


***** Coethnic candidate *****

preserve 

g y=support_candidate
g d=treat
g x=length_nairobi

/* create interaction term */
g dx = d * x

/* linear interaction model */
qui reg y d x dx if coeth_cand==1, robust

mat coefs = e(b)
local coefD = coefs[1,1]
local coefX = coefs[1,2]
local coefDX = coefs[1,3]

mat vcov = e(V)
local varD = vcov[1,1]
local varDX = vcov[3,3]
local covDX = vcov[3,1]

/* calculate linear marginal effect */
qui sum x
local xmin = `r(min)'
local xmax = `r(max)'
local step = (`xmax' - `xmin')/80

mat margeff = J(81,7,.) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames margeff = xlevel marg se CI_l CI_u N Ntr
qui forvalues i = 1/81 {
   mat margeff[`i', 1] = `xmin' + (`i'-1)* `step'
   mat margeff[`i', 2] = `coefD' + `coefDX' * margeff[`i',1]
   mat margeff[`i', 3] = sqrt(`varD' + margeff[`i',1]^2 * `varDX' + `covDX' * margeff[`i',1] * 2)
   mat margeff[`i', 4] = margeff[`i', 2] - 1.96 * margeff[`i', 3]
   mat margeff[`i', 5] = margeff[`i', 2] + 1.96 * margeff[`i', 3]
   /* est +/- 1.96 s.e. only work for relatively large samples */
   count if x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 6] = `r(N)'
   count if d == 1 & x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 7] = `r(N)'
   }
qui count if x == `xmax'
mat margeff[81, 6] = margeff[81, 6] + `r(N)'
qui count if d == 1 & x == `xmax'
mat margeff[81, 7] = margeff[81, 7] + `r(N)'

 /* binning estimator */
local nbins = 3
local ncuts = `nbins'-1
mat cuts = J(`nbins'+1, 1, .)
mat cuts[1,1] = `xmin'
mat cuts[4,1] = `xmax'+ 0.01
qui forvalues i=1/`ncuts' {
   local pct = 100/`nbins'*`i'
   _pctile x, p(`pct')
   mat cuts[(`i'+1),1] = `r(r1)'
 }

mat MEbin = J(`nbins', 5, .) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames MEbin = x0 bin_marg bin_se bin_CI_l bin_CI_u
qui forvalues i = 1/`nbins' {
   g g`i' = (x >= cuts[`i',1] & x < cuts[(`i'+1),1])
   _pctile x if g`i' ==1, p(50)
   mat MEbin[`i', 1] = `r(r1)' 
   g g`i'_d = g`i' * d
   g g`i'_x = g`i' * (x - MEbin[`i',1])
   g g`i'_dx = g`i' * d * (x - MEbin[`i',1])
   }

qui reg y g1_d g2_d g3_d g1 g2 g3 g1_x g2_x g3_x g1_dx g2_dx g3_dx if coeth_cand==1, robust

mat coefs = e(b)
mat vcov = e(V)
qui forvalues i = 1/`nbins' {
   mat MEbin[`i',2] = coefs[1,`i']
   mat MEbin[`i',3] = sqrt(vcov[`i',`i'])
   mat MEbin[`i', 4] = MEbin[`i', 2] - 1.96 * MEbin[`i', 3]
   mat MEbin[`i', 5] = MEbin[`i', 2] + 1.96 * MEbin[`i', 3]
     }

 /* plotting */
svmat margeff, names(col)
keep xlevel marg se CI_l CI_u N Ntr
drop if xlevel == .

/* adjustment for histogram */
qui sum CI_u
local ymax = `r(max)'
qui sum CI_l
local ymin = `r(min)'
local ydiff = `ymax' - `ymin'
local base = `ymin' - `ydiff'/5
display `base'
local base_offset = 5 - abs(`base')
display `base_offset'
*local base = 0
qui sum N
local Nmax = `r(max)'
g N_adj = (N/`Nmax')*`ydiff'/5 + `base' - `base_offset'
g Ntr_adj = (Ntr/`Nmax')*`ydiff'/5 + `base' - `base_offset'

svmat MEbin, names(col)
local xlow = MEbin[1,1]
local xmid = MEbin[2,1]
local xhigh = MEbin[3,1]
local lpos = `ymax' - `ydiff'/10

twoway (rarea CI_u CI_l xlevel, astyle(ci)) ///
  (line marg xlevel, lstyle(p1)) ///
  (bar N_adj xlevel, barwidth(0.4) bfcolor(gs15) blcolor(gs10) base(-5)) ///
  (bar Ntr_adj  xlevel, barwidth(0.4) bfcolor(erose) blcolor(gs8) base(-5)) ///
  (rcap bin_CI_u bin_CI_l x0, lcolor(red)) ///
  (scatter bin_marg x0, mcolor(red) ms(O)) /// 
  , yline(0, lcolor(gs14) lwidth(1)) xlabel(0(10)60) ylabel(-5(1)1) ///
  text(.7 `xlow' "L", color(gs10) size(*1.5)) ///
  text(.7 `xmid' "M", color(gs10) size(*1.5)) ///
  text(.7 `xhigh' "H", color(gs10) size(*1.5)) ///
  title("Coethnic candidate") ///
  xtitle("Years in Nairobi") ytitle("") ///
  ylabel(,ang(h) nogrid)  legend(off)  scheme(s1mono) plotregion(lcolor(white)) aspectratio(1) name(g1, replace) 

restore
  
  
***** Non-coethnic candidate *****

preserve 

g y=support_candidate
g d=treat
g x=length_nairobi

/* create interaction term */
g dx = d * x

/* linear interaction model */
qui reg y d x dx if coeth_cand==0, robust

mat coefs = e(b)
local coefD = coefs[1,1]
local coefX = coefs[1,2]
local coefDX = coefs[1,3]

mat vcov = e(V)
local varD = vcov[1,1]
local varDX = vcov[3,3]
local covDX = vcov[3,1]

/* calculate linear marginal effect */
qui sum x
local xmin = `r(min)'
local xmax = `r(max)'
local step = (`xmax' - `xmin')/80

mat margeff = J(81,7,.) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames margeff = xlevel marg se CI_l CI_u N Ntr
qui forvalues i = 1/81 {
   mat margeff[`i', 1] = `xmin' + (`i'-1)* `step'
   mat margeff[`i', 2] = `coefD' + `coefDX' * margeff[`i',1]
   mat margeff[`i', 3] = sqrt(`varD' + margeff[`i',1]^2 * `varDX' + `covDX' * margeff[`i',1] * 2)
   mat margeff[`i', 4] = margeff[`i', 2] - 1.96 * margeff[`i', 3]
   mat margeff[`i', 5] = margeff[`i', 2] + 1.96 * margeff[`i', 3]
   /* est +/- 1.96 s.e. only work for relatively large samples */
   count if x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 6] = `r(N)'
   count if d == 1 & x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 7] = `r(N)'
   }
qui count if x == `xmax'
mat margeff[81, 6] = margeff[81, 6] + `r(N)'
qui count if d == 1 & x == `xmax'
mat margeff[81, 7] = margeff[81, 7] + `r(N)'

 /* binning estimator */
local nbins = 3
local ncuts = `nbins'-1
mat cuts = J(`nbins'+1, 1, .)
mat cuts[1,1] = `xmin'
mat cuts[4,1] = `xmax'+ 0.01
qui forvalues i=1/`ncuts' {
   local pct = 100/`nbins'*`i'
   _pctile x, p(`pct')
   mat cuts[(`i'+1),1] = `r(r1)'
 }

mat MEbin = J(`nbins', 5, .) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames MEbin = x0 bin_marg bin_se bin_CI_l bin_CI_u
qui forvalues i = 1/`nbins' {
   g g`i' = (x >= cuts[`i',1] & x < cuts[(`i'+1),1])
   _pctile x if g`i' ==1, p(50)
   mat MEbin[`i', 1] = `r(r1)' 
   g g`i'_d = g`i' * d
   g g`i'_x = g`i' * (x - MEbin[`i',1])
   g g`i'_dx = g`i' * d * (x - MEbin[`i',1])
   }

qui reg y g1_d g2_d g3_d g1 g2 g3 g1_x g2_x g3_x g1_dx g2_dx g3_dx if coeth_cand==0, robust

mat coefs = e(b)
mat vcov = e(V)
qui forvalues i = 1/`nbins' {
   mat MEbin[`i',2] = coefs[1,`i']
   mat MEbin[`i',3] = sqrt(vcov[`i',`i'])
   mat MEbin[`i', 4] = MEbin[`i', 2] - 1.96 * MEbin[`i', 3]
   mat MEbin[`i', 5] = MEbin[`i', 2] + 1.96 * MEbin[`i', 3]
   
   }

 /* plotting */
svmat margeff, names(col)
keep xlevel marg se CI_l CI_u N Ntr
drop if xlevel == .

/* adjustment for histogram */
qui sum CI_u
local ymax = `r(max)'
qui sum CI_l
local ymin = `r(min)'
local ydiff = `ymax' - `ymin'
local base = `ymin' - `ydiff'/5
display `base'
local base_offset = 5 - abs(`base')
display `base_offset'
*local base = 0
qui sum N
local Nmax = `r(max)'
g N_adj = (N/`Nmax')*`ydiff'/5 + `base' - `base_offset'
g Ntr_adj = (Ntr/`Nmax')*`ydiff'/5 + `base' - `base_offset'

svmat MEbin, names(col)
local xlow = MEbin[1,1]
local xmid = MEbin[2,1]
local xhigh = MEbin[3,1]
local lpos = `ymax' - `ydiff'/10

twoway (rarea CI_u CI_l xlevel, astyle(ci)) ///
  (line marg xlevel, lstyle(p1)) ///
  (bar N_adj xlevel, barwidth(0.4) bfcolor(gs15) blcolor(gs10) base(-5)) ///
  (bar Ntr_adj  xlevel, barwidth(0.4) bfcolor(erose) blcolor(gs8) base(-5)) ///
  (rcap bin_CI_u bin_CI_l x0, lcolor(red)) ///
  (scatter bin_marg x0, mcolor(red) ms(O)) /// 
  , yline(0, lcolor(gs14) lwidth(1)) xlabel(0(10)60) ylabel(-5(1)1) ///
  text(.7 `xlow' "L", color(gs10) size(*1.5)) ///
  text(.7 `xmid' "M", color(gs10) size(*1.5)) ///
  text(.7 `xhigh' "H", color(gs10) size(*1.5)) ///
  title("Non-coethnic candidate") ///
  xtitle("Years in Nairobi") ytitle("") ///
  ylabel(,ang(h) nogrid)  legend(off)  scheme(s1mono) plotregion(lcolor(white)) aspectratio(1) name(g2, replace) 

restore 

graph combine g1 g2, row(1) scheme(s1mono)
graph export paper_output/figure5.pdf, as(pdf) replace	

  
  
  
  
********************************************************************
**************************** TABLE 1 *******************************
********************************************************************

use "Data/2016 survey data.dta", clear

drop if speech_inclusive==1 | speech_target_sub==1  // drop inclusive appeal and implicit targeted appeal conditions

reg cand_vote2 speech_target_exp, robust
outreg2 using "paper_output/table1", replace  dec(3) 

reg cand_vote2 speech_target_exp##c.length_nairobi, robust 
outreg2 using "paper_output/table1", append  dec(3) 

reg cand_vote2 speech_target_exp##c.contact, robust 
outreg2 using "paper_output/table1", append  dec(3) 

*Note: final table formatted in Word







********************************************************************
******************SUPPLEMENTAL INFORMATION  ************************
********************************************************************



********************************************************************
**************************** TABLE A1 ******************************
********************************************************************

*shows date and sample sizes for surveys used to generate Figure 1 - no code needed




********************************************************************
**************************** TABLE A2 ******************************
********************************************************************

*shows sample sizes by ethnic group for surveys used to generate Figure 1 - no code needed

  
  
  
********************************************************************
**************************** TABLE A3 ******************************
********************************************************************

*shows surnames used in experiment - no code needed

  
  
********************************************************************
**************************** TABLE A4 ******************************
********************************************************************

use "Data/2015 survey data.dta", clear

*code variable
gen cand_eg_actual=0
replace cand_eg_actual=1 if surname=="Mwangi" | surname=="Kamau" | surname=="Nganga" | surname=="Gitau" | surname=="Njuguna" | ///
surname=="Macharia" | surname=="Kimani" | surname=="Karanja" | surname=="Ndungu" | surname=="Waweru"

replace cand_eg_actual=2 if surname=="Onyango" | surname=="Otieno" | surname=="Ochieng" | surname=="Owino" | surname=="Okello" | ///
surname=="Odongo" | surname=="Owuor" | surname=="Ochola" | surname=="Oloo" | surname=="Odero"

replace cand_eg_actual=3 if surname=="Wambua" | surname=="Mutiso" | surname=="Mutuku" | surname=="Muli" | surname=="Kioko" | ///
surname=="Munyao" | surname=="Makau" | surname=="Mutisya" | surname=="Mulwa" | surname=="Musau"

replace cand_eg_actual=4 if surname=="Momanyi" | surname=="Ondieki" | surname=="Makori" | surname=="Nyakundi" | surname=="Mose" | ///
surname=="Mokaya" | surname=="Mogaka" | surname=="Bosire" | surname=="Nyangau" | surname=="Mokua"

replace cand_eg_actual=5 if surname=="Makokha" | surname=="Wafula" | surname=="Barasa" | surname=="Were" | surname=="Wanyonyi" | ///
surname=="Nyongesa" | surname=="Wekesa" | surname=="Simiyu" | surname=="Wanjala" | surname=="Wanyama"

replace cand_eg_actual=6 if surname=="Cheruiyot" | surname=="Chepkwony" | surname=="Langat" | surname=="Chebii" | surname=="Kirui" | ///
surname=="Kibet" | surname=="Ngetich" | surname=="Rono" | surname=="Rotich" | surname=="Kibet"

gen correct_guess=0
replace correct_guess=1 if kikuyu==1 & cand_eg_actual==1 & cand_eg==1
replace correct_guess=1 if kikuyu==1 & cand_eg_actual==1 & cand_eg2==1
replace correct_guess=1 if kikuyu==1 & cand_eg_actual!=1 & cand_eg>1 & cand_eg<998
replace correct_guess=1 if kikuyu==1 & cand_eg_actual!=1 & cand_eg2==2
replace correct_guess=999 if cand_eg==999

replace correct_guess=1 if luo==1 & cand_eg_actual==2 & cand_eg==2
replace correct_guess=1 if luo==1 & cand_eg_actual==2 & cand_eg2==1
replace correct_guess=1 if luo==1 & cand_eg_actual!=2 & cand_eg!=2 & cand_eg<998
replace correct_guess=1 if luo==1 & cand_eg_actual!=2 & cand_eg2==2
replace correct_guess=999 if cand_eg==999

replace correct_guess=1 if kamba==1 & cand_eg_actual==3 & cand_eg==3
replace correct_guess=1 if kamba==1 & cand_eg_actual==3 & cand_eg2==1
replace correct_guess=1 if kamba==1 & cand_eg_actual!=3 & cand_eg!=3 & cand_eg<998
replace correct_guess=1 if kamba==1 & cand_eg_actual!=3 & cand_eg2==2
replace correct_guess=999 if cand_eg==999

replace correct_guess=1 if kisii==1 & cand_eg_actual==4 & cand_eg==4
replace correct_guess=1 if kisii==1 & cand_eg_actual==4 & cand_eg2==1
replace correct_guess=1 if kisii==1 & cand_eg_actual!=4 & cand_eg!=4 & cand_eg<998
replace correct_guess=1 if kisii==1 & cand_eg_actual!=4 & cand_eg2==2
replace correct_guess=999 if cand_eg==999

replace correct_guess=1 if luhya==1 & cand_eg_actual==5 & cand_eg==5
replace correct_guess=1 if luhya==1 & cand_eg_actual==5 & cand_eg2==1
replace correct_guess=1 if luhya==1 & cand_eg_actual!=5 & cand_eg!=5 & cand_eg<998
replace correct_guess=1 if luhya==1 & cand_eg_actual!=5 & cand_eg2==2
replace correct_guess=999 if cand_eg==999

replace correct_guess=1 if kalenjin==1 & cand_eg_actual==6 & cand_eg==6
replace correct_guess=1 if kalenjin==1 & cand_eg_actual==6 & cand_eg2==1
replace correct_guess=1 if kalenjin==1 & cand_eg_actual!=6 & cand_eg!=6 & cand_eg<998
replace correct_guess=1 if kalenjin==1 & cand_eg_actual!=6 & cand_eg2==2
replace correct_guess=999 if cand_eg==999


tab eg correct_guess if target_eg==1, row // this data is used to create Table A4



  
  
********************************************************************
**************************** TABLE A5 ******************************
********************************************************************

drop if target_eg==0

mean age 
mean gender 
mean christian  
mean muslim 
mean education
mean kikuyu 
mean luo 
mean luhya 
mean kamba 
mean kisii 
mean kalenjin 
mean page2has_radio 
mean page2has_tv 


*Note: Comparison data from Afrobarometer Round 5 is available at https://afrobaromter.org
*Note: table formatted in Word


********************************************************************
**************************** TABLE A6 ******************************
********************************************************************

keep if target_eg==1

global varlist age male christian muslim education married_recode kikuyu luo kamba kisii luhya kalenjin wealthindex page2has_radio page2has_tv int_same length_nairobi

balancetable (mean if treat==0) (mean if treat==1) (diff treat)  ///
$varlist using "paper_output/tableA6.xlsx", replace pvalues wide(mean1 mean2 diff3 pval3) nopar format(%12.2fc)


*Note: table formatted in Word



********************************************************************
**************************** TABLE A7 ******************************
********************************************************************

*main test
reg support_candidate i.treat##i.coeth_cand if target_eg==1, robust
outreg2 using "paper_output/tableA7", label dec(2) replace 

*with covariates
reg support_candidate i.treat##i.coeth_cand age ed wealthindex kikuyu luo kamba kisii kalenjin, robust
outreg2 using "paper_output/tableA7", label dec(2) append 

*Note: table formatted in Word







********************************************************************
**************************** TABLE A8 ******************************
********************************************************************


*main test by ethnic group
reg support_candidate i.treat##i.coeth_cand if kikuyu==1, robust
outreg2 using "paper_output/tableA8", label dec(2) replace 

reg support_candidate i.treat##i.coeth_cand if luo==1, robust
outreg2 using "paper_output/tableA8", label dec(2) append 

reg support_candidate i.treat##i.coeth_cand if kamba==1, robust
outreg2 using "paper_output/tableA8", label dec(2) append 

reg support_candidate i.treat##i.coeth_cand if kisii==1, robust
outreg2 using "paper_output/tableA8", label dec(2) append 

reg support_candidate i.treat##i.coeth_cand if luhya==1, robust
outreg2 using "paper_output/tableA8", label dec(2) append 

*Note: table formatted in Word



********************************************************************
**************************** TABLE A9 ******************************
********************************************************************

*secondary outcomes
reg represent_recode i.treat##i.coeth_cand if target_eg==1, robust
outreg2 using "paper_output/tableA9", ctitle("Represent") label dec(2) replace 

reg good_democ_recode i.treat##i.coeth_cand if target_eg==1, robust
outreg2 using "paper_output/tableA9", ctitle("Democracy") label dec(2) append 

reg good_peace_recode i.treat##i.coeth_cand if target_eg==1, robust
outreg2 using "paper_output/tableA9", ctitle("Peace") label dec(2) append 

reg fulfill_promises_recode i.treat##i.coeth_cand if target_eg==1, robust
outreg2 using "paper_output/tableA9", ctitle("Promises") label dec(2) append 

*Note: table formatted in Word





********************************************************************
**************************** TABLE A10 *****************************
********************************************************************


reg support_candidate i.treat##c.length_nairobi if coeth_cand==0 , robust
outreg2 using "paper_output/tableA10", label dec(2) replace 

reg support_candidate i.treat##c.length_nairobi age ed wealthindex unemployed if coeth_cand==0, robust
outreg2 using "paper_output/tableA10", label dec(2) append 

reg support_candidate i.treat##c.length_nairobi i.treat##c.age i.treat##c.ed i.treat##c.wealthindex i.treat##c.unemployed if coeth_cand==0, robust
outreg2 using "paper_output/tableA10", label dec(2) append 

reg support_candidate i.treat##c.length_nairobi if coeth_cand==1, robust
outreg2 using "paper_output/tableA10", label dec(2) append 

reg support_candidate i.treat##c.length_nairobi age ed wealthindex unemployed if coeth_cand==1, robust
outreg2 using "paper_output/tableA10", label dec(2) append 

reg support_candidate i.treat##c.length_nairobi i.treat##c.age i.treat##c.ed i.treat##c.wealthindex i.treat##c.unemployed if coeth_cand==1, robust
outreg2 using "paper_output/tableA10", label dec(2) append 

*Note: table formatted in Word



********************************************************************
**************************** TABLE A11 *****************************
********************************************************************


*shows surnames used in experiment - no code needed





********************************************************************
**************************** TABLE A12 *****************************
********************************************************************

use "Data/2016 survey data.dta", clear

*code variable
gen cand_eg_correct=0
replace cand_eg_correct=1 if eg==cand_eg
replace cand_eg_correct=4 if cand_eg>777
replace cand_eg_correct=5 if cand_eg==999

tab eg cand_eg_correct, row // this data is used to create Table A12




********************************************************************
**************************** TABLE A13 *****************************
********************************************************************


tab cand_eg_correct if speech_control==1
tab cand_eg_correct if speech_inclusive ==1
tab cand_eg_correct if speech_target_exp ==1
tab cand_eg_correct if speech_target_sub ==1

*this data is used to create Table A12





********************************************************************
**************************** TABLE A14 *****************************
********************************************************************

global varlist female age education christian wealthindex length_nairobi married_recode kikuyu luo kamba kisii luhya kalenjin meru int_same

balancetable (mean if treatment==0) (mean if treatment==1) (diff treat_explicit if (treatment!=2 & treatment!=3)) ///
(mean if treatment==2) (diff treat_implicit if (treatment!=1 & treatment!=3)) ///
(mean if treatment==3) (diff treat_inclusive if (treatment!=1 & treatment!=2)) ///
$varlist using "paper_output/tableA14.xlsx" if year==2016, replace pvalues wide(mean1 mean2 diff3 pval3 mean4 diff5 pval5 mean6 diff7 pval7) nopar format(%12.2fc)

*Note: table formatted in Word




********************************************************************
**************************** TABLE A15 *****************************
********************************************************************


reg cand_vote2 speech_target_exp speech_target_sub speech_inclusive, robust
outreg2 using "paper_output/tableA15", replace  dec(2) 

reg cand_vote2  speech_target_exp speech_target_sub speech_inclusive age education wealthindex kikuyu luo kamba kisii kalenjin meru, robust
outreg2 using "paper_output/tableA15", append  dec(2) 

*Note: table formatted in Word




********************************************************************
**************************** FIGURE  A2 ****************************
********************************************************************

preserve 

g y=cand_vote2
g d=speech_target_exp
g x=length_nairobi

/* create interaction term */
g dx = d * x

/* linear interaction model */
drop if speech_inclusive==1 | speech_target_sub==1 // drop of other conditions
qui reg y d x dx, robust

mat coefs = e(b)
local coefD = coefs[1,1]
local coefX = coefs[1,2]
local coefDX = coefs[1,3]

mat vcov = e(V)
local varD = vcov[1,1]
local varDX = vcov[3,3]
local covDX = vcov[3,1]

/* calculate linear marginal effect */
qui sum x
local xmin = `r(min)'
local xmax = `r(max)'
local step = (`xmax' - `xmin')/80

mat margeff = J(81,7,.) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames margeff = xlevel marg se CI_l CI_u N Ntr
qui forvalues i = 1/81 {
   mat margeff[`i', 1] = `xmin' + (`i'-1)* `step'
   mat margeff[`i', 2] = `coefD' + `coefDX' * margeff[`i',1]
   mat margeff[`i', 3] = sqrt(`varD' + margeff[`i',1]^2 * `varDX' + `covDX' * margeff[`i',1] * 2)
   mat margeff[`i', 4] = margeff[`i', 2] - 1.96 * margeff[`i', 3]
   mat margeff[`i', 5] = margeff[`i', 2] + 1.96 * margeff[`i', 3]
   /* est +/- 1.96 s.e. only work for relatively large samples */
   count if x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 6] = `r(N)'
   count if d == 1 & x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 7] = `r(N)'
   }
qui count if x == `xmax'
mat margeff[81, 6] = margeff[81, 6] + `r(N)'
qui count if d == 1 & x == `xmax'
mat margeff[81, 7] = margeff[81, 7] + `r(N)'

 /* binning estimator */
local nbins = 3
local ncuts = `nbins'-1
mat cuts = J(`nbins'+1, 1, .)
mat cuts[1,1] = `xmin'
mat cuts[4,1] = `xmax'+ 0.01
qui forvalues i=1/`ncuts' {
   local pct = 100/`nbins'*`i'
   _pctile x, p(`pct')
   mat cuts[(`i'+1),1] = `r(r1)'
 }

mat MEbin = J(`nbins', 5, .) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames MEbin = x0 bin_marg bin_se bin_CI_l bin_CI_u
qui forvalues i = 1/`nbins' {
   g g`i' = (x >= cuts[`i',1] & x < cuts[(`i'+1),1])
   _pctile x if g`i' ==1, p(50)
   mat MEbin[`i', 1] = `r(r1)' 
   g g`i'_d = g`i' * d
   g g`i'_x = g`i' * (x - MEbin[`i',1])
   g g`i'_dx = g`i' * d * (x - MEbin[`i',1])
   }

qui reg y g1_d g2_d g3_d g1 g2 g3 g1_x g2_x g3_x g1_dx g2_dx g3_dx, robust
/* do not forget to put in the same set of control variables */

mat coefs = e(b)
mat vcov = e(V)
qui forvalues i = 1/`nbins' {
   mat MEbin[`i',2] = coefs[1,`i']
   mat MEbin[`i',3] = sqrt(vcov[`i',`i'])
   mat MEbin[`i', 4] = MEbin[`i', 2] - 1.96 * MEbin[`i', 3]
   mat MEbin[`i', 5] = MEbin[`i', 2] + 1.96 * MEbin[`i', 3]
   
   }

 /* plotting */
svmat margeff, names(col)
keep xlevel marg se CI_l CI_u N Ntr
drop if xlevel == .

/* adjustment for histogram */
qui sum CI_u
local ymax = `r(max)'
qui sum CI_l
local ymin = `r(min)'
local ydiff = `ymax' - `ymin'
local base = `ymin' - `ydiff'/5
*local base = 0
qui sum N
local Nmax = `r(max)'
g N_adj = (N/`Nmax')*`ydiff'/5 + `base'
g Ntr_adj = (Ntr/`Nmax')*`ydiff'/5 + `base'

svmat MEbin, names(col)
local xlow = MEbin[1,1]
local xmid = MEbin[2,1]
local xhigh = MEbin[3,1]
local lpos = `ymax' - `ydiff'/10

twoway (rarea CI_u CI_l xlevel, astyle(ci)) ///
  (line marg xlevel, lstyle(p1)) ///
  (bar N_adj xlevel, barwidth(0.4) bfcolor(gs15) blcolor(gs10) base(`base')) ///
  (bar Ntr_adj  xlevel, barwidth(0.4) bfcolor(erose) blcolor(gs8) base(`base')) ///
  (rcap bin_CI_u bin_CI_l x0, lcolor(red)) ///
  (scatter bin_marg x0, mcolor(red) ms(O)) /// 
  , yline(0, lcolor(gs14) lwidth(1)) xlabel(0(10)70) ylabel(-.8(.2).2) ///
  text(.2 `xlow' "L", color(gs10) size(*1.5)) ///
  text(.2 `xmid' "M", color(gs10) size(*1.5)) ///
  text(.2 `xhigh' "H", color(gs10) size(*1.5)) ///
  xtitle("Years in Nairobi") ytitle("") ///
  ylabel(,ang(h) nogrid)  legend(off)  scheme(s1mono) plotregion(lcolor(white)) aspectratio(1) title("Replication Study (2016)") name(g2, replace)

graph export paper_output/figureA2.pdf, as(pdf) replace	

  
restore




********************************************************************
**************************** TABLE A16 *****************************
********************************************************************


drop if speech_inclusive==1 | speech_target_sub==1 // drop of other conditions

*other specfications for length_nairobi
recode length_nairobi (0/1=0) (.=.) (else=1), gen(nairobi_1)
recode length_nairobi (0/5=0) (.=.) (else=1), gen(nairobi_5)
recode length_nairobi (0/10=0) (.=.) (else=1), gen(nairobi_10) // same as median

reg cand_vote2 i.speech_target_exp##i.nairobi_1 
outreg2 using "paper_output/tableA16", replace  dec(2) 

reg cand_vote2 i.speech_target_exp##i.nairobi_5 
outreg2 using "paper_output/tableA16", append  dec(2) 

reg cand_vote2 i.speech_target_exp##i.nairobi_10 
outreg2 using "paper_output/tableA16", append  dec(2) 

*Note: table formatted in Word





********************************************************************
**************************** TABLE A17 *****************************
********************************************************************

use "Data/2015 survey data.dta", clear
drop if target_eg==0

*2SLS
gen coeth_guess=0
replace coeth_guess=1 if kikuyu==1  & cand_eg==1
replace coeth_guess=1 if kikuyu==1  & cand_eg2==1

replace coeth_guess=1 if luo==1  & cand_eg==2
replace coeth_guess=1 if luo==1  & cand_eg2==1

replace coeth_guess=1 if kamba==1 & cand_eg==3
replace coeth_guess=1 if kamba==1 & cand_eg2==1

replace coeth_guess=1 if kisii==1 & cand_eg==4
replace coeth_guess=1 if kisii==1 & cand_eg2==1

replace coeth_guess=1 if luhya==1 & cand_eg==5
replace coeth_guess=1 if luhya==1 & cand_eg2==1

replace coeth_guess=1 if kalenjin==1 & cand_eg==6
replace coeth_guess=1 if kalenjin==1 & cand_eg2==1
label var coeth_guess "Candidate thought to be coethnic"

* int_ta: interaction with the assigned (coeth_cand)
gen int_ta = treat*coeth_cand
* int_tg: interaction with the coeth_guess and treat
gen int_tg=treat*coeth_guess

*CACE (use coeth_cand as an instrument for the correct guess)
ivregress 2sls support_candidate treat (coeth_guess = coeth_cand), robust
outreg2 using "paper_output/tableA17", replace  dec(2) 

ivregress 2sls support_candidate treat (coeth_guess int_tg = coeth_cand int_ta), robust
outreg2 using "paper_output/tableA17", append  dec(2) 







********************************************************************
**************************** TABLE A18 *****************************
********************************************************************

*2015
use "Data/2015 survey data.dta", clear
drop if target_eg==0
reg support_candidate i.treat##i.int_match if coeth_cand==1, robust 
outreg2 using "paper_output/tableA18", replace  dec(2) 


*2016
use "Data/2016 survey data.dta", clear
drop if speech_inclusive==1 | speech_target_sub==1 | county==2 // drop other conditions

rename cand_vote2 support_candidate // consistent variable names
rename speech_target_exp treat
rename int_same int_match

reg support_candidate i.treat##i.int_match, robust 
outreg2 using "paper_output/tableA18", append  dec(2) 

*Note: table formatted in Word


  

********************************************************************
**************************** TABLE A19 *****************************
********************************************************************


***** 2015 *****
use "Data/2015 survey data.dta", clear
drop if target_eg==0

reg support_candidate i.treat##c.group_share if coeth_cand==1, robust
outreg2 using "paper_output/tableA19", replace  dec(2) 

reg support_candidate i.treat##i.plurality_group if coeth_cand==1, robust
outreg2 using "paper_output/tableA19", append  dec(2) 


***** 2016 *****
use "Data/2016 survey data.dta", clear

drop if speech_inclusive==1 | speech_target_sub==1 | county==2 // drop other conditions

rename cand_vote2 support_candidate // consistent variable names
rename speech_target_exp treat
rename int_same int_match

gen plurality_group=0
replace plurality_group=1 if group_share==largest_share

reg support_candidate i.treat##c.group_share, robust
outreg2 using "paper_output/tableA19", append  dec(2) 

reg support_candidate i.treat##i.plurality_group, robust
outreg2 using "paper_output/tableA19", append  dec(2) 

*Note: table formatted in Word





********************************************************************
**************************** FIGURE A3 *****************************
********************************************************************



*** 2015 ***

use "Data/2015 survey data.dta", clear
drop if target_eg==0

g y=support_candidate
g d=treat
g x=group_share

/* create interaction term */
g dx = d * x

/* linear interaction model */
qui reg y d x dx if coeth_cand==1, robust

mat coefs = e(b)
local coefD = coefs[1,1]
local coefX = coefs[1,2]
local coefDX = coefs[1,3]

mat vcov = e(V)
local varD = vcov[1,1]
local varDX = vcov[3,3]
local covDX = vcov[3,1]

/* calculate linear marginal effect */
qui sum x
local xmin = `r(min)'
local xmax = `r(max)'
local step = (`xmax' - `xmin')/80

mat margeff = J(81,7,.) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames margeff = xlevel marg se CI_l CI_u N Ntr
qui forvalues i = 1/81 {
   mat margeff[`i', 1] = `xmin' + (`i'-1)* `step'
   mat margeff[`i', 2] = `coefD' + `coefDX' * margeff[`i',1]
   mat margeff[`i', 3] = sqrt(`varD' + margeff[`i',1]^2 * `varDX' + `covDX' * margeff[`i',1] * 2)
   mat margeff[`i', 4] = margeff[`i', 2] - 1.96 * margeff[`i', 3]
   mat margeff[`i', 5] = margeff[`i', 2] + 1.96 * margeff[`i', 3]
   /* est +/- 1.96 s.e. only work for relatively large samples */
   count if x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 6] = `r(N)'
   count if d == 1 & x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 7] = `r(N)'
   }
qui count if x == `xmax'
mat margeff[81, 6] = margeff[81, 6] + `r(N)'
qui count if d == 1 & x == `xmax'
mat margeff[81, 7] = margeff[81, 7] + `r(N)'

 /* binning estimator */
local nbins = 3
local ncuts = `nbins'-1
mat cuts = J(`nbins'+1, 1, .)
mat cuts[1,1] = `xmin'
mat cuts[4,1] = `xmax'+ 0.01
qui forvalues i=1/`ncuts' {
   local pct = 100/`nbins'*`i'
   _pctile x, p(`pct')
   mat cuts[(`i'+1),1] = `r(r1)'
 }

mat MEbin = J(`nbins', 5, .) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames MEbin = x0 bin_marg bin_se bin_CI_l bin_CI_u
qui forvalues i = 1/`nbins' {
   g g`i' = (x >= cuts[`i',1] & x < cuts[(`i'+1),1])
   _pctile x if g`i' ==1, p(50)
   mat MEbin[`i', 1] = `r(r1)' 
   g g`i'_d = g`i' * d
   g g`i'_x = g`i' * (x - MEbin[`i',1])
   g g`i'_dx = g`i' * d * (x - MEbin[`i',1])
   }

qui reg y g1_d g2_d g3_d g1 g2 g3 g1_x g2_x g3_x g1_dx g2_dx g3_dx if coeth_cand==1, robust
/* do not forget to put in the same set of control variables */

mat coefs = e(b)
mat vcov = e(V)
qui forvalues i = 1/`nbins' {
   mat MEbin[`i',2] = coefs[1,`i']
   mat MEbin[`i',3] = sqrt(vcov[`i',`i'])
   mat MEbin[`i', 4] = MEbin[`i', 2] - 1.96 * MEbin[`i', 3]
   mat MEbin[`i', 5] = MEbin[`i', 2] + 1.96 * MEbin[`i', 3]
   
   }

 /* plotting */
svmat margeff, names(col)
keep xlevel marg se CI_l CI_u N Ntr
drop if xlevel == .

/* adjustment for histogram */
qui sum CI_u
local ymax = `r(max)'
qui sum CI_l
local ymin = `r(min)'
local ydiff = `ymax' - `ymin'
local base = `ymin' - `ydiff'/5
display `base'
local base_offset = 3 - abs(`base')
display `base_offset'
*local base = 0
qui sum N
local Nmax = `r(max)'
g N_adj = (N/`Nmax')*`ydiff'/5 + `base' - `base_offset'
g Ntr_adj = (Ntr/`Nmax')*`ydiff'/5 + `base' - `base_offset'


svmat MEbin, names(col)
local xlow = MEbin[1,1]
local xmid = MEbin[2,1]
local xhigh = MEbin[3,1]
local lpos = `ymax' - `ydiff'/10

twoway (rarea CI_u CI_l xlevel, astyle(ci)) ///
  (line marg xlevel, lstyle(p1)) ///
  (bar N_adj xlevel, barwidth(0.005) bfcolor(gs15) blcolor(gs10) base(-3)) ///
  (bar Ntr_adj  xlevel, barwidth(0.005) bfcolor(erose) blcolor(gs8) base(-3)) ///
  (rcap bin_CI_u bin_CI_l x0, lcolor(red)) ///
  (scatter bin_marg x0, mcolor(red) ms(O)) /// 
  , yline(0, lcolor(gs14) lwidth(1)) xlabel(0(.1).6) ylabel(-3(1)1) ///
  text(.7 `xlow' "L", color(gs10) size(*1.5)) ///
  text(.7 `xmid' "M", color(gs10) size(*1.5)) ///
  text(.7 `xhigh' "H", color(gs10) size(*1.5)) ///
  title("Initial Study (2015)") ///
  xtitle("Group Share") ytitle("") ///
  ylabel(,ang(h) nogrid)  legend(off)  scheme(s1mono) plotregion(lcolor(white)) aspectratio(1) name(g1, replace) 

 graph export paper_output/figureA3_1.pdf, as(pdf) replace	

  


***** 2016 *****
  
use "Data/2016 survey data.dta", clear

drop if speech_inclusive==1 | speech_target_sub==1 | county==2 // drop other conditions

g y=cand_vote2
g d=speech_target_exp
g x=group_share

/* create interaction term */
g dx = d * x

/* linear interaction model */
qui reg y d x dx, robust

* note that you can
* (1) put in additional control variables;
* (2) use other type of standard errors
* (3) use other estimators, such as areg, xtreg, etc.

mat coefs = e(b)
local coefD = coefs[1,1]
local coefX = coefs[1,2]
local coefDX = coefs[1,3]

mat vcov = e(V)
local varD = vcov[1,1]
local varDX = vcov[3,3]
local covDX = vcov[3,1]

/* calculate linear marginal effect */
qui sum x
local xmin = `r(min)'
local xmax = `r(max)'
local step = (`xmax' - `xmin')/80

mat margeff = J(81,7,.) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames margeff = xlevel marg se CI_l CI_u N Ntr
qui forvalues i = 1/81 {
   mat margeff[`i', 1] = `xmin' + (`i'-1)* `step'
   mat margeff[`i', 2] = `coefD' + `coefDX' * margeff[`i',1]
   mat margeff[`i', 3] = sqrt(`varD' + margeff[`i',1]^2 * `varDX' + `covDX' * margeff[`i',1] * 2)
   mat margeff[`i', 4] = margeff[`i', 2] - 1.96 * margeff[`i', 3]
   mat margeff[`i', 5] = margeff[`i', 2] + 1.96 * margeff[`i', 3]
   /* est +/- 1.96 s.e. only work for relatively large samples */
   count if x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 6] = `r(N)'
   count if d == 1 & x >= margeff[`i', 1] & x < (margeff[`i', 1] + `step')
   mat margeff[`i', 7] = `r(N)'
   }
qui count if x == `xmax'
mat margeff[81, 6] = margeff[81, 6] + `r(N)'
qui count if d == 1 & x == `xmax'
mat margeff[81, 7] = margeff[81, 7] + `r(N)'

 /* binning estimator */
local nbins = 3
local ncuts = `nbins'-1
mat cuts = J(`nbins'+1, 1, .)
mat cuts[1,1] = `xmin'
mat cuts[4,1] = `xmax'+ 0.01
qui forvalues i=1/`ncuts' {
   local pct = 100/`nbins'*`i'
   _pctile x, p(`pct')
   mat cuts[(`i'+1),1] = `r(r1)'
 }

mat MEbin = J(`nbins', 5, .) /* x level, marginal effect, SE, lower CI, upper CI, N, Ntr */
mat colnames MEbin = x0 bin_marg bin_se bin_CI_l bin_CI_u
qui forvalues i = 1/`nbins' {
   g g`i' = (x >= cuts[`i',1] & x < cuts[(`i'+1),1])
   _pctile x if g`i' ==1, p(50)
   mat MEbin[`i', 1] = `r(r1)' 
   g g`i'_d = g`i' * d
   g g`i'_x = g`i' * (x - MEbin[`i',1])
   g g`i'_dx = g`i' * d * (x - MEbin[`i',1])
   }

qui reg y g1_d g2_d g3_d g1 g2 g3 g1_x g2_x g3_x g1_dx g2_dx g3_dx, robust
/* do not forget to put in the same set of control variables */

mat coefs = e(b)
mat vcov = e(V)
qui forvalues i = 1/`nbins' {
   mat MEbin[`i',2] = coefs[1,`i']
   mat MEbin[`i',3] = sqrt(vcov[`i',`i'])
   mat MEbin[`i', 4] = MEbin[`i', 2] - 1.96 * MEbin[`i', 3]
   mat MEbin[`i', 5] = MEbin[`i', 2] + 1.96 * MEbin[`i', 3]
   
   }

 /* plotting */
svmat margeff, names(col)
keep xlevel marg se CI_l CI_u N Ntr
drop if xlevel == .

/* adjustment for histogram */
qui sum CI_u
local ymax = `r(max)'
qui sum CI_l
local ymin = `r(min)'
local ydiff = `ymax' - `ymin'
local base = `ymin' - `ydiff'/5
display `base'
qui sum N
local Nmax = `r(max)'
g N_adj = (N/`Nmax')*`ydiff'/5 + `base' -.25
g Ntr_adj = (Ntr/`Nmax')*`ydiff'/5 + `base' -.25

svmat MEbin, names(col)
local xlow = MEbin[1,1]
local xmid = MEbin[2,1]
local xhigh = MEbin[3,1]
local lpos = `ymax' - `ydiff'/10

twoway (rarea CI_u CI_l xlevel, astyle(ci)) ///
  (line marg xlevel, lstyle(p1)) ///
  (bar N_adj xlevel, barwidth(0.005) bfcolor(gs15) blcolor(gs10) base(-.6)) ///
  (bar Ntr_adj  xlevel, barwidth(0.005) bfcolor(erose) blcolor(gs8) base(-.6)) ///
  (rcap bin_CI_u bin_CI_l x0, lcolor(red)) ///
  (scatter bin_marg x0, mcolor(red) ms(O)) /// 
  , yline(0, lcolor(gs14) lwidth(1)) xlabel(0(.1).6) ylabel(-.6(.1).3) ///
  text(.2 `xlow' "L", color(gs10) size(*1.5)) ///
  text(.2 `xmid' "M", color(gs10) size(*1.5)) ///
  text(.2 `xhigh' "H", color(gs10) size(*1.5)) ///
  xtitle("Group share") ytitle("") ///
  ylabel(,ang(h) nogrid)  legend(off)   scheme(s1mono) plotregion(lcolor(white)) aspectratio(1) ///
  title("Replication Study (2016)") name(g2, replace) 

graph export paper_output/figureA3_2.pdf, as(pdf) replace	








